package org.hackystat.iw.projectoverviewer;

import org.apache.wicket.protocol.http.WicketServlet;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;

/**
 * Provides a mechanism for running Project Overviewer with Jetty.
 * 
 * @author Philip Johnson
 * @author Jianfei Liao
 */
public class Start {

  /**
   * Run the project overviewer with Jetty.
   * 
   * @param args Ignored.
   * @throws Exception If problems occur.
   */
  public static void main(String[] args) throws Exception {
    // default port is 8081
    int port = 8081;
    // first command line argument is the port number
    if (args.length > 0) {
      port = Integer.parseInt(args[0]);
    }
    Server server = new Server(port);
    Context context = new Context(server, "/projectoverviewer", Context.SESSIONS);
    
    ServletHolder servletHolder = new ServletHolder(new WicketServlet());
    servletHolder.setInitParameter("applicationClassName",
        "org.hackystat.iw.projectoverviewer.ProjectOverviewerApplication");
    servletHolder.setInitOrder(1);
    context.addServlet(servletHolder, "/*");
    
    context.setResourceBase("build/war"); // this is needed
    server.addHandler(context);

    
    try {
      System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP");
      server.start();
      while (System.in.available() == 0) {
        Thread.sleep(5000);
      }
      server.stop();
      server.join();
    }
    catch (Exception e) {
      e.printStackTrace();
      System.exit(100);
    }
  }
}
